package com.gordon.tips.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.apache.commons.io.FileUtils;

import com.gordon.tips.log.Logger;
import com.gordon.tips.log.LoggerImpl;
import com.gordon.tips.util.ReadResource;




/**
 * upload action
 * @author Gordon
 *
 */
public class UploadAction extends BaseAction {
	//the file to upload
	private File upload;
	private String uploadFileContentType;
	private String uploadFileName;	
	
	//file upload folder
	private String targetDir;
	
	//target name
	private String disName;
	
	//logger component
	private static Logger logger = new LoggerImpl(UploadAction.class);
	
	//output the file name
	private String resultFileName;
	private String result;
	

	
	
	
	

	/**
	 * upload core action
	 * @Author: Gordon
	 */
	public String uploadFile() throws Exception {		
		logger.debug("uploading..");
		logger.debug("upload file name="+uploadFileName);
		logger.debug(upload);
		
		//handle the ext
		String ext="";
		if(uploadFileName.contains("."))ext=uploadFileName.substring(uploadFileName.indexOf("."));
		
		//upload file
		String targetFileName = disName+ext;
		String s=request.getContextPath();
		logger.debug(s);
		String targetFolder=ReadResource.getProperties("projectPath");
		File targetDirFile = new File(targetFolder+targetDir);
		if (!targetDirFile.exists()) {
			targetDirFile.mkdirs();
		}
		File picFile = new File(targetDirFile, targetFileName);
		try {
			FileUtils.copyFile(upload, picFile);
		} catch (IOException e) {
			logger.error("copy file error.");
			e.printStackTrace();
		}	
		resultFileName=targetDir+"/"+targetFileName;
		logger.debug("resultFileName="+resultFileName);
		return result;
	}
	
	/**
	 * @return
	 * @Author: Gordon
	 */
	public String test()
	{
		logger.debug("testing");
		logger.debug(targetDir);
		return null;
	}

	
	//getter and setter
	public String getTargetDir() {
		return targetDir;
	}

	public void setTargetDir(String targetDir) {
		this.targetDir = targetDir;
	}

	public String getDisName() {
		return disName;
	}

	public void setDisName(String disName) {
		this.disName = disName;
	}


	public File getUpload() {
		return upload;
	}


	public void setUpload(File upload) {
		this.upload = upload;
	}


	public String getUploadFileContentType() {
		return uploadFileContentType;
	}


	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}


	public String getUploadFileName() {
		return uploadFileName;
	}


	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getResultFileName() {
		return resultFileName;
	}

	public void setResultFileName(String resultFileName) {
		this.resultFileName = resultFileName;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	

}
